<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Area;
use App\Models\Cts;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class CtsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
            return  $content->header('旅行社')
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Cts::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->tools(function ($tools) {
                $tools->append(new ImportButton('cts'));
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('title','名称');
                $filter->equal('area_id','区县选择')->select(Area::all()->pluck('title', 'id'));
            });
            $grid->id('ID')->sortable();
            $grid->title('名称');
            $grid->area_id('区县')->editable('select', Area::all()->pluck('title','id'));
            $grid->legal_person('法人');
            $grid->address('地址');
            $grid->licence('许可证编号');
            $grid->credit_code('社会信用代码');
            $grid->credit_record('信用记录')->editable('select', config('options.cts_credit_record'));;
            $grid->level('评级')->editable('select', config('options.cts_level'));
            $grid->mot_date('年检');
            $grid->status('运营状态')->display(function ($val){
                return $val?config('options.status')[$val]:'运营中';
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Cts::class, function (Form $form) {
            $form->select('area_id','区县')->options(Area::all()->pluck('title','id'));
            $form->text('title','旅行社中文名称');
            $form->select('level','评级')->options(config('options.cts_level'));
            $form->text('legal_person','法人');
            $form->text('business','许可经营业务');
            $form->text('address','地址');
            $form->number('credit_score','信用分值')->max(10);
            $form->text('licence','许可证编号');
            $form->text('credit_code','社会信用代码');
            $form->select('credit_record','信用记录')->options(config('options.cts_credit_record'));
        });
    }
}
